package com.oedtech.esl.config.timer;

import com.oedtech.esl.repository.esl.EslTabDao;
import com.oedtech.esl.service.nettyService.DataService;
import com.oedtech.esl.socket.BsChannelMap;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.Set;


/**
 * SN--定时检查是否有掉线标签，有则每5分钟重新下发，共6次，然后每2小时下发，工24小时，再每24小时下发，共72小时
 */
@Component
@Configurable
@EnableScheduling
public class PushTimer {
    private final static Logger LOGGER= LoggerFactory.getLogger(PushTimer.class);
    @Autowired
    private DataService dataService;
    Map<String,Channel> channelMap= BsChannelMap.getInstance();
    @Scheduled(cron="${ap.push_img_cron}")
    public void timer(){
        Set<String>apidSet=channelMap.keySet();

//        eslTabDao.retryImgFlash();  //对更新失败且重试小于3次的价签，将push_times置1，重新下发数据

        for (String apid : apidSet) {
                try {
                    dataService.sendImgCmd(apid,channelMap.get(apid), null);
                } catch (IOException e) {
                   LOGGER.error("pushtimer error in 'sendImgCmd' method",e);
                }
        }

    }
}
